package com.dianping.cat.alarm.rule.transform;

import com.dianping.cat.alarm.rule.entity.Condition;
import com.dianping.cat.alarm.rule.entity.Config;
import com.dianping.cat.alarm.rule.entity.MetricItem;
import com.dianping.cat.alarm.rule.entity.MonitorRules;
import com.dianping.cat.alarm.rule.entity.Rule;
import com.dianping.cat.alarm.rule.entity.SubCondition;
import java.util.Map;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
















public class DefaultDomMaker
  extends Object
  implements IMaker<Node>
{
  public Condition buildCondition(Node node) {
/*  34 */     String minute = getAttribute(node, "minute");
/*  35 */     String title = getAttribute(node, "title");
    String alertType = getAttribute(node, "alertType");

/*  38 */     Condition condition = new Condition();

/*  40 */     if (minute != null) {
/*  41 */       condition.setMinute((Integer)convert(Integer.class, minute, null));
    }

/*  44 */     if (title != null) {
/*  45 */       condition.setTitle(title);
    }

/*  48 */     if (alertType != null) {
/*  49 */       condition.setAlertType(alertType);
    }

/*  52 */     return condition;
  }


  public Config buildConfig(Node node) {
/*  57 */     String starttime = getAttribute(node, "starttime");
/*  58 */     String endtime = getAttribute(node, "endtime");

/*  60 */     Config config = new Config();

/*  62 */     if (starttime != null) {
/*  63 */       config.setStarttime(starttime);
    }

/*  66 */     if (endtime != null) {
/*  67 */       config.setEndtime(endtime);
    }

/*  70 */     return config;
  }


  public MetricItem buildMetricItem(Node node) {
/*  75 */     String monitorCount = getAttribute(node, "monitorCount");
/*  76 */     String monitorSum = getAttribute(node, "monitorSum");
/*  77 */     String monitorAvg = getAttribute(node, "monitorAvg");
    String metricItemText = getAttribute(node, "metricItemText");
/*  79 */     String productText = getAttribute(node, "productText");

/*  81 */     MetricItem metricItem = new MetricItem();

/*  83 */     if (monitorCount != null) {
/*  84 */       metricItem.setMonitorCount((Boolean)convert(Boolean.class, monitorCount, null));
    }

/*  87 */     if (monitorSum != null) {
/*  88 */       metricItem.setMonitorSum((Boolean)convert(Boolean.class, monitorSum, null));
    }

/*  91 */     if (monitorAvg != null) {
/*  92 */       metricItem.setMonitorAvg((Boolean)convert(Boolean.class, monitorAvg, null));
    }

/*  95 */     if (metricItemText != null) {
/*  96 */       metricItem.setMetricItemText(metricItemText);
    }

/*  99 */     if (productText != null) {
/* 100 */       metricItem.setProductText(productText);
    }

/* 103 */     return metricItem;
  }



/* 108 */   public MonitorRules buildMonitorRules(Node node) { return new MonitorRules(); }





  public Rule buildRule(Node node) {
/* 115 */     String id = getAttribute(node, "id");

/* 117 */     Rule rule = new Rule(id);

/* 119 */     Map<String, String> dynamicAttributes = rule.getDynamicAttributes();
/* 120 */     NamedNodeMap attributes = node.getAttributes();
/* 121 */     int length = (attributes == null) ? 0 : attributes.getLength();

/* 123 */     for (int i = 0; i < length; i++) {
/* 124 */       Node item = attributes.item(i);

/* 126 */       dynamicAttributes.put(item.getNodeName(), item.getNodeValue());
    }

/* 129 */     dynamicAttributes.remove("id");

/* 131 */     return rule;
  }


  public SubCondition buildSubCondition(Node node) {
/* 136 */     String type = getAttribute(node, "type");
/* 137 */     String text = getAttribute(node, "text");

/* 139 */     SubCondition subCondition = new SubCondition();

/* 141 */     if (type != null) {
/* 142 */       subCondition.setType(type);
    }

/* 145 */     if (text != null) {
/* 146 */       subCondition.setText(text);
    }

/* 149 */     return subCondition;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 154 */     if (value == null || value.length() == 0) {
/* 155 */       return defaultValue;
    }

/* 158 */     if (type == Boolean.class || type == boolean.class)
/* 159 */       return (T)Boolean.valueOf(value); 
/* 160 */     if (type == Integer.class || type == int.class)
/* 161 */       return (T)Integer.valueOf(value); 
/* 162 */     if (type == Long.class || type == long.class)
/* 163 */       return (T)Long.valueOf(value); 
/* 164 */     if (type == Short.class || type == short.class)
/* 165 */       return (T)Short.valueOf(value); 
/* 166 */     if (type == Float.class || type == float.class)
/* 167 */       return (T)Float.valueOf(value); 
/* 168 */     if (type == Double.class || type == double.class)
/* 169 */       return (T)Double.valueOf(value); 
/* 170 */     if (type == Byte.class || type == byte.class)
/* 171 */       return (T)Byte.valueOf(value); 
/* 172 */     if (type == Character.class || type == char.class) {
/* 173 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 175 */     return (T)value;
  }


  protected String getAttribute(Node node, String name) {
/* 180 */     Node attribute = node.getAttributes().getNamedItem(name);

/* 182 */     return (attribute == null) ? null : attribute.getNodeValue();
  }

  protected Node getChildTagNode(Node parent, String name) {
/* 186 */     NodeList children = parent.getChildNodes();
/* 187 */     int len = children.getLength();

/* 189 */     for (int i = 0; i < len; i++) {
/* 190 */       Node child = children.item(i);

/* 192 */       if (child.getNodeType() == 1 && 
/* 193 */         child.getNodeName().equals(name)) {
/* 194 */         return child;
      }
    }


/* 199 */     return null;
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/rule/transform/DefaultDomMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */